package com.zuipin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.DeliveryAddress;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IDeliveryAddressService;
import com.zuipin.util.Pagination;
import com.zuipin.util.RegexUtils;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.DeliveryAddressVo;
import com.zuipin.vo.UpdateDeliveryAddressVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 
 * @ClassName: DeliveryAddressServiceImpl 
 * @author wuyicheng
 * @time 2017年3月27日下午4:01:46 
 */
@RestController
@Api(value = "收货地址信息", description = "收货地址信息-wuyicheng")
@RequestMapping(value = "/member/deliveryAddress")
public class DeliveryAddressController {
	@Resource
	private IDeliveryAddressService deliveryAddressService;
	
	@RequestMapping(value = "/findById", method = RequestMethod.GET)
	@ApiOperation(value = "根据id查询收货地址-wuyicheng", notes = "根据id查询收货地址-wuyicheng")
	@Valids({ @Valid(name = "deliveryAddressId", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "deliveryAddressId", value = "收货地址ID", required = true, dataType = "string", paramType = "query"), })
	public Result<DeliveryAddress> findById(@RequestParam Long deliveryAddressId) {
		Result<DeliveryAddress> result = new Result<>();
		if (deliveryAddressId != null) {
			DeliveryAddress deliveryAddress = deliveryAddressService.findById(deliveryAddressId);
			if (deliveryAddress != null) {
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setData(deliveryAddress);
			} else {
				result.setErrorCode(ErrorCode.NOT_FIND);
				result.setData(null);
			}
		} else {
			result.setErrorCode(ErrorCode.NULL_POINT);
			result.setData(null);
		}
		return result;
	}
	
	@RequestMapping(value = "/addDeliveryAddress", method = RequestMethod.POST)
	@ApiOperation(value = "新增收货地址-wuyicheng", notes = "新增收货地址-wuyicheng")
	@Valids({ @Valid(name = "deliveryAddressVo.recName", required = true, minLength = 0, maxLength = 20), @Valid(name = "deliveryAddressVo.mobilePhone", required = false),
			@Valid(name = "deliveryAddressVo.phone", required = false), @Valid(name = "deliveryAddressVo.provinceCode", required = true, regex = RegexUtils.CODE),
			@Valid(name = "deliveryAddressVo.cityCode", required = true, regex = RegexUtils.CODE),
			@Valid(name = "deliveryAddressVo.areaCode", required = true, regex = RegexUtils.CODE),
			@Valid(name = "deliveryAddressVo.detailAddress", required = true, minLength = 0, maxLength = 200),
			@Valid(name = "deliveryAddressVo.isDefault", required = true, minValue = 0), @Valid(name = "deliveryAddressVo.zipCode", required = false),
			@Valid(name = "deliveryAddressVo.memberId", required = true, minValue = 0), @Valid(name = "deliveryAddressVo.sysId", required = true, minValue = 0), })
	public Result<Map<String, Object>> addDeliveryAddress(@RequestBody @ApiParam(value = "收货地址") DeliveryAddressVo deliveryAddressVo) {
		Result<Map<String, Object>> result = new Result<>();
		DeliveryAddress deliveryAddress = new DeliveryAddress();
		boolean isPhoneNotNull = validateMobilePhoneAndPhone(deliveryAddressVo.getMobilePhone(), deliveryAddressVo.getPhone());
		if (isPhoneNotNull) {
			BeanUtils.copyProperties(deliveryAddressVo, deliveryAddress);
			deliveryAddressService.insert(deliveryAddress);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setData(null);
		} else {
			result.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			// 手机和电话必须填写其中一个
			result.setMessage("mobile phone errors or phone errors");
			result.setData(null);
		}
		return result;
	}
	
	/** 
	 * 验证手机和电话是否存在一个，且符合格式
	 * 
	 * @Title: validateMobilePhoneAndPhone 
	 * @author: wuyicheng
	 * @date: 2017年5月3日下午6:06:21 
	 * @param mobilePhone
	 * @param phone
	 * @return
	 * @return: boolean
	 * @throws 
	 */
	// private boolean validateMobilePhoneAndPhone(String mobilePhone, String phone) {
	// //手机不能等于空且符合手机格式，或者电话不为空且符合电话格式
	// if ((StringUtil.isNotBlank(mobilePhone) && RegexUtils.isMobile(mobilePhone))
	// || (StringUtil.isNotBlank(phone) && RegexUtils.isPhone(phone))) {
	// return true;
	// }
	// return false;
	// }
	
	/** 
	 * 验证手机和电话是否存在一个，且符合格式
	 * 
	 * @Title: validateMobilePhoneAndPhone 
	 * @author: wuyicheng
	 * @date: 2017年5月3日下午6:06:21 
	 * @param mobilePhone
	 * @param phone
	 * @return
	 * @return: boolean
	 * @throws 
	 */
	private boolean validateMobilePhoneAndPhone(String mobilePhone, String phone) {
		// 手机不能等于空且符合手机格式，或者电话不为空且符合电话格式
		// 同时为空
		if ((StringUtil.isBlank(mobilePhone)) && (StringUtil.isBlank(phone))) {
			return false;
		}
		if (StringUtil.isNotBlank(mobilePhone)) {
			// 不符合格式
			if (!RegexUtils.isMobileFuzzy(mobilePhone)) {
				return false;
			}
		}
		if (StringUtil.isNotBlank(phone)) {
			// 不符合格式
			if (!RegexUtils.isTelePhone(phone)) {
				return false;
			}
		}
		
		return true;
	}
	
	@RequestMapping(value = "/updateDeliveryAddress", method = RequestMethod.POST)
	@ApiOperation(value = "更新收货地址-wuyicheng", notes = "更新收货地址-wuyicheng")
	@Valids({ @Valid(name = "updateDeliveryAddressVo.recName", required = true, minLength = 0, maxLength = 20),
			@Valid(name = "updateDeliveryAddressVo.mobilePhone", required = false), @Valid(name = "updateDeliveryAddressVo.phone", required = false),
			@Valid(name = "updateDeliveryAddressVo.provinceCode", required = true, regex = RegexUtils.CODE),
			@Valid(name = "updateDeliveryAddressVo.cityCode", required = true, regex = RegexUtils.CODE),
			@Valid(name = "updateDeliveryAddressVo.areaCode", required = true, regex = RegexUtils.CODE),
			@Valid(name = "updateDeliveryAddressVo.detailAddress", required = true, minLength = 0, maxLength = 200),
			@Valid(name = "updateDeliveryAddressVo.isDefault", required = true, minValue = 0), @Valid(name = "updateDeliveryAddressVo.zipCode", required = false),
			@Valid(name = "updateDeliveryAddressVo.memberId", required = true, minValue = 0), @Valid(name = "updateDeliveryAddressVo.sysId", required = true, minValue = 0),
			@Valid(name = "updateDeliveryAddressVo.id", required = true, minValue = 0), })
	public Result<Map<String, Object>> updateDeliveryAddress(@RequestBody @ApiParam(value = "收货地址") UpdateDeliveryAddressVo updateDeliveryAddressVo) throws CustomException {
		Result<Map<String, Object>> result = new Result<>();
		
		if (updateDeliveryAddressVo != null) {
			
			/*boolean isPhoneNotNull = validateMobilePhoneAndPhone(
					updateDeliveryAddressVo.getMobilePhone(), updateDeliveryAddressVo.getPhone());
			if (isPhoneNotNull) {*/
			DeliveryAddress deliveryAddress = deliveryAddressService.findById(updateDeliveryAddressVo.getId());
			if (deliveryAddress != null) {
				BeanUtils.copyProperties(updateDeliveryAddressVo, deliveryAddress);
				deliveryAddressService.updateDeliveryAddress(deliveryAddress);
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setData(null);
			} else {
				result.setErrorCode(ErrorCode.NOT_FIND);
				result.setData(null);
			}
			/*} else {
				result.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				//手机和电话必须填写其中一个
				result.setMessage("mobile phone errors or phone errors");
				result.setData(null);
			}*/
		} else {
			result.setErrorCode(ErrorCode.NULL_POINT);
			result.setData(null);
		}
		return result;
	}
	
	@RequestMapping(value = "/deleteDeliveryAddress", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除收货地址-wuyicheng", notes = "删除收货地址-wuyicheng")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), @Valid(name = "deliveryAddressId", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "deliveryAddressId", value = "收货地址ID", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> deleteDeliveryAddress(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam Long deliveryAddressId) throws CustomException {
		Result<Map<String, Object>> result = new Result<>();
		result = deliveryAddressService.deleteAddress(memberId, sysId, deliveryAddressId, result);
		return result;
	}
	
	@RequestMapping(value = "/updateDefauleAddress", method = RequestMethod.POST)
	@ApiOperation(value = "设置默认收货地址-wuyicheng", notes = "设置默认收货地址-wuyicheng")
	@Valids({ @Valid(name = "deliveryAddressId", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "deliveryAddressId", value = "收货地址ID", required = true, dataType = "long", paramType = "query"), })
	public Result<Map<String, Object>> updateDefauleAddress(@RequestParam Long deliveryAddressId) throws CustomException {
		Result<Map<String, Object>> result = new Result<>();
		if (deliveryAddressId != null) {
			DeliveryAddress deliveryAddress = deliveryAddressService.findById(deliveryAddressId);
			if (deliveryAddress != null) {
				deliveryAddress.setIsDefault(DeliveryAddress.IS_DEFUALT_YES_1);
				deliveryAddressService.updateDeliveryAddress(deliveryAddress);
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setData(null);
			} else {
				result.setErrorCode(ErrorCode.NOT_FIND);
				result.setData(null);
			}
		} else {
			result.setErrorCode(ErrorCode.NULL_POINT);
			result.setData(null);
		}
		return result;
	}
	
	@RequestMapping(value = "/searchDeliveryAddress", method = RequestMethod.GET)
	@ApiOperation(value = "查询收货地址-wuyicheng", notes = "查询收货地址-wuyicheng")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), @Valid(name = "pageNumber", required = true),
			@Valid(name = "pageSize", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "pageNumber", value = "页码", required = true, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int", paramType = "query"), })
	public PageResult<List<DeliveryAddress>> searchDeliveryAddress(@RequestParam Long memberId, @RequestParam Long sysId, Integer pageNumber, Integer pageSize) {
		PageResult<List<DeliveryAddress>> result = new PageResult<>();
		Pagination page = new Pagination();
		page.setPageSize(pageSize);
		page.setPageNumber(pageNumber);
		DeliveryAddress deliveryAddress = new DeliveryAddress();
		deliveryAddress.setMemberId(memberId);
		deliveryAddress.setSysId(sysId);
		List<DeliveryAddress> list = deliveryAddressService.findListByMemberIdAndSysId(deliveryAddress, page);
		if (list != null && list.size() > 0) {
			result.setTotal_record(list.size());
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(list);
		return result;
	}
	
	@RequestMapping(value = "/searchDefauleAddress", method = RequestMethod.GET)
	@ApiOperation(value = "查询默认收货地址-wuyicheng", notes = "查询默认收货地址-wuyicheng")
	public Result<DeliveryAddress> searchDefauleAddress(@ApiParam(required = true, name = "memberId", value = "会员id") @RequestParam Long memberId,
			@ApiParam(required = true, name = "sysId", value = "系统id") @RequestParam Long sysId) {
		Result<DeliveryAddress> result = new Result<>();
		DeliveryAddress deliveryAddress = deliveryAddressService.findDefault(memberId, sysId);
		// if (deliveryAddress != null) {
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(deliveryAddress);
		// } else {
		// result.setErrorCode(ErrorCode.NOT_FIND);
		// result.setData(null);
		// }
		return result;
	}
	
}
